package cn.amos.mvc.web;

import cn.amos.mvc.common.api.MobileApi;
import cn.amos.mvc.common.util.RetrofitUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import retrofit2.Call;
import retrofit2.Retrofit;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.GZIPInputStream;

/**
 * PROJECT: mvc
 * DESCRIPTION: note
 *
 * @author Daoyuan
 * @date 2019/1/4
 */
@RestController
@RequestMapping("mobile")
public class MobileController {

    private MobileApi mobileApi;

    private static final Map<String, String> HEADER = new HashMap<>(4);

    private void initMoxie() {
        HEADER.put("Authorization", "Token 78ddd536203c404b888f05dd482dc4c4");
        Retrofit retrofit = new RetrofitUtil("https://api.51datakey.com/", HEADER).initRetrofit();
        mobileApi = retrofit.create(MobileApi.class);
    }

    @GetMapping("hello/{mobile}")
    public String hello(@PathVariable("mobile") String mobile) throws IOException {
        String requestUrl = MessageFormat.format("https://api.51datakey.com/carrier/v3/mobiles/{0}/mxreport", mobile);

        URL url = new URL(requestUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
        connection.setRequestProperty("Accept", "application/json");
        connection.setRequestProperty("Authorization", "Token 78ddd536203c404b888f05dd482dc4c4");
        connection.setRequestProperty("Content-type", "application/json;charset=UTF-8");
        connection.connect();
        String responseBody = uncompress(connection.getInputStream());
        connection.disconnect();

        return responseBody;
    }

    private static String uncompress(InputStream gzippedResponse) throws IOException {
        InputStream decompressedResponse = new GZIPInputStream(gzippedResponse);
        Reader reader = new InputStreamReader(decompressedResponse, StandardCharsets.UTF_8);
        StringWriter writer = new StringWriter();

        char[] buffer = new char[10240];
        for (int length; (length = reader.read(buffer)) > 0; ) {
            writer.write(buffer, 0, length);
        }

        writer.close();
        reader.close();
        decompressedResponse.close();
        gzippedResponse.close();

        return writer.toString();
    }

    @GetMapping("hello2/{mobile}")
    public JSONObject hello2(@PathVariable("mobile") String mobile) throws IOException {
        initMoxie();
        // OkHttp
        Call<JSONObject> call = mobileApi.getReportBasicString(mobile);
        retrofit2.Response<JSONObject> execute = call.execute();
        if (HttpStatus.OK.value() == execute.code()) {
            System.out.println(execute.body());
            return execute.body();
        }

        return null;
    }

}